﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Js_Study.DownNotOpen
{
    public partial class Down : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_down_Click(object sender, EventArgs e)
        {
            string dowmFilePath = "http://az370354.vo.msecnd.net/videos/PGA-TOUR-uses-Dynamics-Marketing-to-Drive-Sponsor-Satisfacti.mp4";

            WebClient wcClient = new WebClient();

            string sp = "d:\\s";
            if (!Directory.Exists(sp))
            {
                Directory.CreateDirectory(sp);
            }

            wcClient.DownloadFile(dowmFilePath, sp + "\\my.mp4");

            //WebRequest webReq = WebRequest.Create(dowmFilePath);

            //WebResponse webRes = webReq.GetResponse();

            //long fileLength = webRes.ContentLength;

            //Stream srm = webRes.GetResponseStream();

            //StreamReader srmReader = new StreamReader(srm);

            //byte[] bufferbyte = new byte[fileLength];

            //int allByte = (int)bufferbyte.Length;

            //int startByte = 0;

            //while (fileLength > 0)
            //{
            //    int downByte = srm.Read(bufferbyte, startByte, allByte);
            //    if (downByte == 0 )
            //    {
            //        break;
            //    }
            //    startByte += downByte;
            //    allByte -= downByte;
            //}

            //if (!Directory.Exists(savePath))
            //{
            //    Directory.CreateDirectory(savePath);
            //}

            //FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //fs.Write(bufferbyte, 0, bufferbyte.Length);
            //srm.Close();
            //srmReader.Close();
            //fs.Close();
            //FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //fs.Write(bufferbyte, 0, bufferbyte.Length);


            //MemoryStream ms = new MemoryStream(bufferbyte);


            //string path = dowmFilePath;
            //FileStream fs = new FileStream(path, FileMode.Open);
            //fs.Read(bufferbyte, 0, bufferbyte.Length);
            //fs.Close();

            //Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开
            //Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("yourFile.mp4", System.Text.Encoding.UTF8));
            //Response.BinaryWrite(bufferbyte);
            //Response.Flush();
            //Response.End(); 
        }
    }
}